package com.lcredu.furn.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcredu.furn.bean.Furn;
import com.lcredu.furn.response.Result;
import com.lcredu.furn.service.FurnService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author 黎长容
 * @date 2025-08-04 19:48
 */
@Controller
public class FurnController {
    @Resource
    private FurnService furnService;

    /*
         缺少@ResponseBody   HTTP状态 404 - 未找到
         缺少@RequestBody     HTTP状态 500 - 内部服务器错误
     */
    @ResponseBody
    @PostMapping("/save")
    public Result save(@Validated @RequestBody Furn furn, Errors errors) {
        HashMap<String, Object> errorMap = new HashMap<>();
        //将错误传入
        if (errors.hasErrors()) {
            for (FieldError error : errors.getFieldErrors()) {
                errorMap.put(error.getField(), error.getDefaultMessage());
            }
            return Result.fail().add("errorMap", errorMap);
        } else {
            //没有错误
            furnService.save(furn);
            return Result.success();
        }

    }

    //返回全部数据
    @RequestMapping("/findAll")
    @ResponseBody
    public Result findAll() {
        System.out.println("被调用...");
        List<Furn> furns = furnService.findAll();
        System.out.println(furns);
        return Result.success().add("furns", furns);
    }

    //修改家具数据
    @ResponseBody
    @PutMapping("/updateFurn")
    public Result updateFurn(@RequestBody Furn furn) {
        furnService.editFurn(furn);
        return Result.success();
    }

    //查询单个家居
    @ResponseBody
    @GetMapping("/findFurnById/{id}")
    public Result findFurnById(@PathVariable Integer id) {
        Furn furn = furnService.getFurnById(id);
        return Result.success().add("furn", furn);
    }

    //删除家居
    @ResponseBody
    @DeleteMapping("/deleteFurn/{id}")
    public Result deleteFurn(@PathVariable Integer id) {
        furnService.deleteFurn(id);
        return Result.success();
    }

    //分页处理
    @ResponseBody
    @GetMapping("/listFurnsByPage")
    public Result listFurnsByPage(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        //查询前，需要调用 PageHelper.startPage()
        //看下源码 startPage(int pageNum, int pageSize)
        PageHelper.startPage(pageNum, pageSize);
        //在 PageHelper.startPage() 后调用 findAll 就是分页查询(物理分页有 limit)
        List<Furn> furnList = furnService.findAll();
        //分页查询完之后，可以使用 pageInfo 来包装查询后的结果，
        //1. 只需要将 pageInfo 交给页面就行
        //2. pageInfo 封装了详细的分页信息，包括查询出来的数据
        // 比如总共有多少页，当前是第几页等
        //3. 看源码 PageInfo(List<T> list, int navigatePages)
        PageInfo pageInfo = new PageInfo(furnList, pageSize);
        return Result.success().add("pageInfo", pageInfo);
    }

    //根据名字条件进行分页
    @ResponseBody
    @GetMapping("/listFurnsByName")
    public Result listFurnsByName(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "5") Integer pageSize,
                                  @RequestParam(required = false, defaultValue = "") String search) {
        System.out.println("search=" + search);
        PageHelper.startPage(pageNum, pageSize);
        List<Furn> furnList = furnService.findByCondition(search);
        PageInfo pageInfo = new PageInfo(furnList, pageSize);
        return Result.success().add("pageInfo", pageInfo);
    }
}
